﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace cart.utils
{
    public class DB
    {
        /// <summary>
        /// 得到数据库连接
        /// ousinka 2009年12月15日9:22:34
        /// </summary>
        /// <returns></returns>
        public static SqlConnection getSqlConnection() 
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlConnectionString"].ConnectionString);
            return conn;
        }

        /// <summary>
        /// 打开连接
        /// ousinka 2009年12月15日9:22:59
        /// </summary>
        /// <param name="conn"></param>
        public static void connectionOpen(SqlConnection conn)
        {
            try {
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
            }
            catch
            {
                conn.Close();
            }
        }

        /// <summary>
        /// 关闭数据库连接
        /// ousinka 2009年12月15日9:23:28
        /// </summary>
        /// <param name="conn"></param>
        public static void connectionClose(SqlConnection conn)
        {
            try
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
            catch 
            {
                conn.Close();
            }
        }

        /// <summary>
        /// 执行一条没有返回的sql语句
        /// ousinka 2009年12月15日9:24:15
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static Boolean executeNonQuerySQL(string sql)
        {
            SqlConnection conn = null;
            try
            {
                conn = getSqlConnection();
                connectionOpen(conn);
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.ExecuteNonQuery();
                connectionClose(conn);
                return true;
            }
            catch 
            {
                connectionClose(conn);
                return false;
            }
        }

        /// <summary>
        /// 执行一条有参数但没有返回的sql语句
        /// ousinka 2009年12月21日
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="prams"></param>
        /// <returns></returns>
        public static Boolean executeNonQuerySQL(string sql,SqlParameter [] prams)
        {
            SqlConnection conn = null;
            try
            {
                conn = getSqlConnection();
                connectionOpen(conn);
                SqlCommand cmd = new SqlCommand(sql, conn);

                if (prams != null)
                {
                    foreach (SqlParameter parameter in prams)
                    {
                        cmd.Parameters.Add(parameter);
                    }
                } 


                cmd.ExecuteNonQuery();
                connectionClose(conn);
                return true;
            }
            catch(Exception e)
            {
                connectionClose(conn);
                return false;
            }
        }
    }
}
